﻿using System;
using System.Threading;
using UnityEngine;

// Token: 0x02000309 RID: 777
public class TextureScale
{
	// Token: 0x060012F6 RID: 4854 RVA: 0x0009A080 File Offset: 0x00098280
	public static void Point(Texture2D tex, int newWidth, int newHeight)
	{
		global::TextureScale.ThreadedScale(tex, newWidth, newHeight, false);
	}

	// Token: 0x060012F7 RID: 4855 RVA: 0x0009A08C File Offset: 0x0009828C
	public static void Bilinear(Texture2D tex, int newWidth, int newHeight)
	{
		global::TextureScale.ThreadedScale(tex, newWidth, newHeight, true);
	}

	// Token: 0x060012F8 RID: 4856 RVA: 0x0009A098 File Offset: 0x00098298
	private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
	{
		global::TextureScale.texColors = tex.GetPixels();
		global::TextureScale.newColors = new Color[newWidth * newHeight];
		if (useBilinear)
		{
			global::TextureScale.ratioX = 1f / ((float)newWidth / (float)(tex.width - 1));
			global::TextureScale.ratioY = 1f / ((float)newHeight / (float)(tex.height - 1));
		}
		else
		{
			global::TextureScale.ratioX = (float)tex.width / (float)newWidth;
			global::TextureScale.ratioY = (float)tex.height / (float)newHeight;
		}
		global::TextureScale.w = tex.width;
		global::TextureScale.w2 = newWidth;
		int num = Mathf.Min(SystemInfo.processorCount, newHeight);
		int num2 = newHeight / num;
		global::TextureScale.finishCount = 0;
		if (global::TextureScale.mutex == null)
		{
			global::TextureScale.mutex = new Mutex(false);
		}
		if (num > 1)
		{
			int i;
			global::TextureScale.ThreadData threadData;
			for (i = 0; i < num - 1; i++)
			{
				threadData = new global::TextureScale.ThreadData(num2 * i, num2 * (i + 1));
				ParameterizedThreadStart start = (!useBilinear) ? new ParameterizedThreadStart(global::TextureScale.PointScale) : new ParameterizedThreadStart(global::TextureScale.BilinearScale);
				Thread thread = new Thread(start);
				thread.Start(threadData);
			}
			threadData = new global::TextureScale.ThreadData(num2 * i, newHeight);
			if (useBilinear)
			{
				global::TextureScale.BilinearScale(threadData);
			}
			else
			{
				global::TextureScale.PointScale(threadData);
			}
			while (global::TextureScale.finishCount < num)
			{
				Thread.Sleep(1);
			}
		}
		else
		{
			global::TextureScale.ThreadData obj = new global::TextureScale.ThreadData(0, newHeight);
			if (useBilinear)
			{
				global::TextureScale.BilinearScale(obj);
			}
			else
			{
				global::TextureScale.PointScale(obj);
			}
		}
		tex.Resize(newWidth, newHeight);
		tex.SetPixels(global::TextureScale.newColors);
		tex.Apply();
	}

	// Token: 0x060012F9 RID: 4857 RVA: 0x0009A22C File Offset: 0x0009842C
	public static void BilinearScale(object obj)
	{
		global::TextureScale.ThreadData threadData = (global::TextureScale.ThreadData)obj;
		for (int i = threadData.start; i < threadData.end; i++)
		{
			int num = (int)Mathf.Floor((float)i * global::TextureScale.ratioY);
			int num2 = num * global::TextureScale.w;
			int num3 = (num + 1) * global::TextureScale.w;
			int num4 = i * global::TextureScale.w2;
			for (int j = 0; j < global::TextureScale.w2; j++)
			{
				int num5 = (int)Mathf.Floor((float)j * global::TextureScale.ratioX);
				float value = (float)j * global::TextureScale.ratioX - (float)num5;
				global::TextureScale.newColors[num4 + j] = global::TextureScale.ColorLerpUnclamped(global::TextureScale.ColorLerpUnclamped(global::TextureScale.texColors[num2 + num5], global::TextureScale.texColors[num2 + num5 + 1], value), global::TextureScale.ColorLerpUnclamped(global::TextureScale.texColors[num3 + num5], global::TextureScale.texColors[num3 + num5 + 1], value), (float)i * global::TextureScale.ratioY - (float)num);
			}
		}
		global::TextureScale.mutex.WaitOne();
		global::TextureScale.finishCount++;
		global::TextureScale.mutex.ReleaseMutex();
	}

	// Token: 0x060012FA RID: 4858 RVA: 0x0009A368 File Offset: 0x00098568
	public static void PointScale(object obj)
	{
		global::TextureScale.ThreadData threadData = (global::TextureScale.ThreadData)obj;
		for (int i = threadData.start; i < threadData.end; i++)
		{
			int num = (int)(global::TextureScale.ratioY * (float)i) * global::TextureScale.w;
			int num2 = i * global::TextureScale.w2;
			for (int j = 0; j < global::TextureScale.w2; j++)
			{
				global::TextureScale.newColors[num2 + j] = global::TextureScale.texColors[(int)((float)num + global::TextureScale.ratioX * (float)j)];
			}
		}
		global::TextureScale.mutex.WaitOne();
		global::TextureScale.finishCount++;
		global::TextureScale.mutex.ReleaseMutex();
	}

	// Token: 0x060012FB RID: 4859 RVA: 0x0009A41C File Offset: 0x0009861C
	private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
	{
		return new Color(c1.r + (c2.r - c1.r) * value, c1.g + (c2.g - c1.g) * value, c1.b + (c2.b - c1.b) * value, c1.a + (c2.a - c1.a) * value);
	}

	// Token: 0x040018BD RID: 6333
	protected static Color[] texColors;

	// Token: 0x040018BE RID: 6334
	protected static Color[] newColors;

	// Token: 0x040018BF RID: 6335
	protected static int w;

	// Token: 0x040018C0 RID: 6336
	protected static float ratioX;

	// Token: 0x040018C1 RID: 6337
	protected static float ratioY;

	// Token: 0x040018C2 RID: 6338
	protected static int w2;

	// Token: 0x040018C3 RID: 6339
	protected static int finishCount;

	// Token: 0x040018C4 RID: 6340
	protected static Mutex mutex;

	// Token: 0x0200030A RID: 778
	public class ThreadData
	{
		// Token: 0x060012FC RID: 4860 RVA: 0x0009A494 File Offset: 0x00098694
		public ThreadData(int s, int e)
		{
			this.start = s;
			this.end = e;
		}

		// Token: 0x040018C5 RID: 6341
		public int start;

		// Token: 0x040018C6 RID: 6342
		public int end;
	}
}
